package com.nuaa.dao.change.impl;

import java.io.File;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.stereotype.Repository;

import com.nuaa.dao.base.impl.BaseDaoImpl;
import com.nuaa.dao.change.inter.SupplyDaoInter;
import com.nuaa.domain.change.SupplyEntity;
import com.nuaa.utils.AjaxMsg;
/***
 * 补充材料Dao
 * @author heaven
 *
 */
@Repository("supplyDao")
public class SupplyDaoImpl extends BaseDaoImpl<SupplyEntity, Integer> implements SupplyDaoInter{
	@Transactional
	public AjaxMsg saveSupplyInfo(SupplyEntity entity,String customer_no){
		AjaxMsg msg = new AjaxMsg();
		//entity.setCustomer_no(customer_no);
		try{
			this.save(entity);
			msg.setSuccess(true);
			msg.setMsg("提交补充材料成功！");
		}catch(Exception e){
			e.printStackTrace();
			msg.setSuccess(false);
			msg.setMsg("提交补充材料失败！");
		}
		return msg;
	}
	@Transactional
	@Override
	public boolean delByOrderNo(String orderNo) {
		StringBuilder sql=new StringBuilder();
		sql.append("select * from sup_info where order_no = '");
		sql.append(orderNo);
		sql.append("'");
		List<SupplyEntity> list = this.search(SupplyEntity.class, sql.toString());
		if(list.size()==0)
			return false;
		SupplyEntity entity = list.get(0);
		String dir = System.getProperty("webApp.path")+"file";
		System.out.println(dir);
		if(!"".equals(entity.getNew_barcode_name())){
			File file = new File(dir+"\\"+entity.getNew_barcode_name());
			if(file.exists()){
				file.delete();
			}
		}
		if(!"".equals(entity.getNew_old_name())){
			File file = new File(dir+"\\"+entity.getNew_old_name());
			if(file.exists()){
				file.delete();
			}
		}
		if(!"".equals(entity.getOut_tip_name())){
			File file = new File(dir+"\\"+entity.getOut_tip_name());
			if(file.exists()){
				file.delete();
			}
		}
		if(!"".equals(entity.getOther_file())){
			File file = new File(dir+"\\"+entity.getOther_file());
			if(file.exists()){
				file.delete();
			}
		}
		if(!"".equals(entity.getRemove_order_name())){
			File file = new File(dir+"\\"+entity.getRemove_order_name());
			if(file.exists()){
				file.delete();
			}
		}
//		System.out.println("~~~4");
//		StringBuffer sql2 = new StringBuffer();
//		sql.append("delete from sup_info where order_no ='");
//		sql.append(orderNo);
//		sql.append("'");
		try{
			//this.update(sql2.toString());
			this.delete(entity);
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		return true;
	}
	@Override
	public void delFileByName(String fileName) {
		String dir = System.getProperty("webApp.path")+"file";
		if(!"".equals(fileName)){
			File file = new File(dir+"\\"+fileName);
			if(file.exists()){
				file.delete();
			}
		}
	}
}
